package com.kh.first.service;

import com.kh.first.config.WxCpConfiguration;
import com.kh.first.dao.RepairLogDao;
import com.kh.first.pojo.DepartmentTotalPojo;
import com.kh.first.pojo.PipeChartPojo;
import com.kh.first.pojo.RepairLogPojo;
import com.kh.first.pojo.RepairStaffTotalPojo;
import com.kh.first.utils.Tools;
import com.kh.first.utils.UserUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RepairLogService {

	@Autowired
	private RepairLogDao repairlogDao;

	public Long saveRepairLog(RepairLogPojo pojo) throws WxErrorException {
		Integer logType = pojo.getLogType();
		if (1 == logType) {
			commonNotify(pojo);
		} else if (2 == logType) {
			mesNotify(pojo);
		}
		return repairlogDao.saveRepairLog(pojo);
	}

	private void mesNotify(RepairLogPojo pojo) throws WxErrorException {
		Integer agentId = 1000017;// Tools.appConfig.getAgentId();
		final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
		NewArticle article = new NewArticle();
		article.setTitle(pojo.getApplyStaffName() + " 部门: " + pojo.getApplyStaffDepartment());
		article.setDescription("报修原因:\n" + pojo.getApplyReason());
//        article.setPicUrl(pojo.get);
		WxCpMessage message = WxCpMessage.NEWS().agentId(agentId) // 企业号应用ID
				.addArticle(article)
//                .("报修人: "+pojo.getApplyStaffName())
//                .btnTxt("报修原因:")
//                .description(pojo.getApplyReason())
				.toUser("ChenZeHua|Wangqingqi")
//                .toParty("非必填，PartyID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
//                .toTag("非必填，TagID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
				.build();
		wxCpService.messageSend(message);
	}
	private void commonNotify(RepairLogPojo pojo) throws WxErrorException {
		Integer agentId = 1000004;// Tools.appConfig.getAgentId();
		final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
		NewArticle article = new NewArticle();
		article.setTitle(pojo.getApplyStaffName() + " 部门: " + pojo.getApplyStaffDepartment());
		article.setDescription("报修原因:\n" + pojo.getApplyReason());
//        article.setPicUrl(pojo.get);
		WxCpMessage message = WxCpMessage.NEWS().agentId(agentId) // 企业号应用ID
				.addArticle(article)
//                .("报修人: "+pojo.getApplyStaffName())
//                .btnTxt("报修原因:")
//                .description(pojo.getApplyReason())
				.toUser("ChenZeHua|qianjiaqi|wangbei|Wangqingqi|1165867687@qq.com")
//                .toParty("非必填，PartyID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
//                .toTag("非必填，TagID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
				.build();
		wxCpService.messageSend(message);
	}

	public Long saveRepairLogList(List<RepairLogPojo> list) {
		return repairlogDao.saveRepairLogList(list);
	}

	public Long deleteRepairLogById(Long id) {
		return repairlogDao.deleteRepairLogById(id);
	}

	public Long updateRepairLogById(RepairLogPojo pojo0) throws WxErrorException {
		Integer rate = pojo0.getRate();
		if (null != rate) {
			RepairLogPojo repairLogPojo = new RepairLogPojo();
			repairLogPojo.setId(pojo0.getId());
			RepairLogPojo pojo = getRepairLogById(repairLogPojo);
			Integer agentId = 1000004;// Tools.appConfig.getAgentId();
			final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
			NewArticle article = new NewArticle();
			article.setTitle(pojo.getApplyStaffName() + " 部门: " + pojo.getApplyStaffDepartment());
			article.setDescription(
					"报修原因:\n" + pojo.getApplyReason() + "\n评分: " + rate + "\n评分内容: " + pojo.getRateContent());
//        article.setPicUrl(pojo.get);
			WxCpMessage message = WxCpMessage.NEWS().agentId(agentId) // 企业号应用ID
					.addArticle(article)
//                .("报修人: "+pojo.getApplyStaffName())
//                .btnTxt("报修原因:")
//                .description(pojo.getApplyReason())
					.toUser("ChenZeHua|qianjiaqi|wangbei|Wangqingqi|1165867687@qq.com")
//                .toParty("非必填，PartyID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
//                .toTag("非必填，TagID列表，多个接受者用‘|’分隔。当touser为@all时忽略本参数")
					.build();
			wxCpService.messageSend(message);
		}
		return repairlogDao.updateRepairLogById(pojo0);
	}

	public List<RepairLogPojo> getRepairLogList(RepairLogPojo pojo) {
		return repairlogDao.getRepairLogList(pojo);
	}

	public Long countRepairLogList(RepairLogPojo pojo) {
		return repairlogDao.countRepairLogList(pojo);
	}

	public Long countRepairGroupByDepartment(DepartmentTotalPojo pojo) {
		return repairlogDao.countRepairGroupByDepartment(pojo);
	}

	public List<DepartmentTotalPojo> getRepairGroupByDepartment(DepartmentTotalPojo pojo) {
		return repairlogDao.getRepairGroupByDepartment(pojo);
	}

	public List<RepairStaffTotalPojo> getRepairLogGroupByRepairType(RepairStaffTotalPojo pojo) {
		return repairlogDao.getRepairLogGroupByRepairType(pojo);
	}

	public RepairLogPojo getRepairLogById(RepairLogPojo pojo) {
		return repairlogDao.getRepairLogById(pojo);
	}

	public List<Map<String, Object>> getYearLine(Integer year) {
		return repairlogDao.getYearLine(year);
	}

	public List<PipeChartPojo> findMesTypeCount(RepairLogPojo repairLogPojo) {
		return repairlogDao.findMesTypeCount(repairLogPojo);
	}
//    public RepairLogPojo getRepairLogById(Long id) {
//        return repairlogDao.getRepairLogById(id);
//    }
}
